package com.dtwave.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 */
public class TimeUtils {

    private static Logger LOGGER = LoggerFactory.getLogger(TimeUtils.class);

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYYMMddHHmmSS = "yyyy/MM/dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";


    public static Date getCurDate() {
        return new Date();
    }

    public static String getCurDateStr() {
        return new SimpleDateFormat("yyyyMMdd").format(getCurDate());
    }

    public static String formatCurDateTime() {
        Instant formDb = Instant.now();
        ZonedDateTime beijing = ZonedDateTime.ofInstant(formDb, ZoneId.systemDefault());
        return beijing.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
    }

    public static long daysDiff(String startDate, String endDate) {

        try {
            if (startDate != null && "".equals(startDate.trim()) && endDate != null && "".equals(endDate.trim())) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDD);
                Calendar cal = Calendar.getInstance();
                cal.setTime(simpleDateFormat.parse(startDate));
                long s = cal.getTimeInMillis();
                cal.setTime(simpleDateFormat.parse(endDate));
                long e = cal.getTimeInMillis();
                return (s - e) / (1000 * 3600 * 24);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
